package hy.planegame.sprite

import android.graphics.Canvas
import android.graphics.Rect
import hy.planegame.utils.boomBm
import hy.planegame.utils.paint

/**
 * Created time : 2017/8/31 16:57.
 * 爆炸效果
 * @author HY
 */
class Boom : Sprite<Boom>() {
    private val width = boomBm.width / 3 * 2
    private val height = boomBm.height / 3 * 2

    private val singleWidth = width / 14

    private val dstRect = Rect()

    private var pos = 0
    private var centerX = 0
    private var centerY = 0

    private var count = 1//延迟计数器


    override fun draw(canvas: Canvas?) {
        if (null == canvas) return
        if (isDraw) {
            val x = centerX - singleWidth / 2 - singleWidth * pos
            dstRect.set(x,
                    centerY - height / 2,
                    x + width,
                    centerY + height / 2)

            canvas.save()
            canvas.clipRect(centerX - singleWidth / 2, centerY - height / 2, centerX + singleWidth / 2, centerY + height / 2)
            canvas.drawBitmap(boomBm, null, dstRect, paint)
            canvas.restore()
        }
    }

    var isBooming = false
    override fun logic() {
        if (isDraw) {
            if (count % 2 == 0)
                if (pos < 14) {

                    pos++
                } else {
                    _onStop?.invoke(this)
                }

            count++
        }else{
            _onStop?.invoke(this)
        }

    }


    private var isDraw = false

    fun start(centerX: Int, centerY: Int) {
        this.centerX = centerX
        this.centerY = centerY
        isBooming = true
        isDraw = true
    }

    fun reset() {
        pos = 0
        count = 1
        isDraw = false
        isBooming = false
    }

    override fun destroy() {
        //销毁
        isDraw = false
    }
}